/*
 * This file is part of SSHTTPD.
 *
 * SSHTTPD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SSHTTPD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** @file
 * Misc functions/macros/crap used thruought the application but are not 
 * big enough to merit a catagory yet.
 */

/* App includes. */
#include "include/utils.h"

/* System level includes. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>

void event(int type, char *format, ...) {
	va_list args;
	time_t ltime;
	struct tm *tm;
	char tbuff[32];
	
	/* Get the time. */
	ltime = time(NULL);
	tm = localtime(&ltime);
	strftime(tbuff, 32, "%b %d %Y %H:%M:%S", tm);
	
	/* event calls now support printf style variable args and formaters. */
	va_start(args, format);
	
	fprintf(EVENTOUT, "%s %d ", tbuff, getpid());
	switch(type) {
		case EWARN:
		case EPROB:
			fprintf(EVENTOUT, "problem: ");
		case ELOG:
		case ECON:
			vfprintf(EVENTOUT, format, args);
			fprintf(EVENTOUT, "\n"); /* New line. */
			break;
	}
	va_end(args);
}

char *heapstr(char *str) {
	char *hstr;
	
	if(str == NULL)
		return NULL;
	
	hstr = malloc(strlen(str) + 1);
	strcpy(hstr, str);
	return hstr;
}
